
public class RegressionIndividual extends Individual {
	int[] genome;
	double [][]data;
	
	public RegressionIndividual(double [][]data){
		super();
		this.genome=new int[35];
		this.initGenome();
		this.data=data;
	}
	
	protected void initGenome(){
		for(int i=0;i<35;i=i+1)
			this.genome[i]=(int)(Math.random()*2);
		                                                     
	}
	protected double evaluate(){
		int[] arr= coefficient();
		double[] yi=new double[data.length];
		
		for (int j=0;j<data.length;j=j+1){
			yi[j]=arr[0]*Math.pow(data[j][0],5)+arr[1]*Math.pow(data[j][0],4)
						+arr[2]*Math.pow(data[j][0],3)+arr[3]*Math.pow(data[j][0],1)+arr[4];				
		}//j
		return fitness(yi);
		
		
		
	}
	
	private double fitness(double[] yi){
		double sum=0;
		for(int i=0;i<this.data.length;i=i+1){
			sum=sum+Math.pow((yi[i]-this.data[i][1]),2);		
		}
		sum=sum/(this.data.length);
		return sum;
	}
			
		
/**
	 * calculate the coffocients of the equation
	 *the method transfers binary numbers into integer
	 *@param int[]genome the array with the cofficients,int[]arr the array with integer cofficients
	 */						
	
		
	private int[] coefficient(){
		int[]arr=new int[5];
		int coefficient=0;
		int i=0;
		while(i<35){
			int count=0;
			for(int j=0;j<7;j=j+1){
				count=count+(int)(Math.pow(2,j))*this.genome[j+i];
			}
			arr[coefficient]=count;
			coefficient=coefficient+1;
			i=i+7;	
		}
		return arr;
		
	}//coefficient
			
public RegressionIndividual copy(){
		RegressionIndividual ans= new RegressionIndividual(data);
		for(int i=0;i<this.genome.length;i=i+1)
			ans.genome[i]=this.genome[i];
		ans.fitness=this.fitness;
		return ans;
		
	
}			
	
public Individual mutate(){
RegressionIndividual mu= (RegressionIndividual)(this.replicate());
int mutetone=(int)(Math.random()*35);
mu.genome[mutetone]=(int)(Math.random()*2);
return mu;
}
public Individual crossover(Individual other){
	int i=choosenum();
	RegressionIndividual ans=(RegressionIndividual)(this.replicate());
for (int m=i;m<35;m=m+1)
	ans.genome[m]=((RegressionIndividual)other).genome[m];
return ans;	
	
}		

private int choosenum(){

int num=(int)(Math.random()*3);
if (num==0)
	return 7;
else{
	if(num==1)
		return 14;
	else
		return 21;
			
} 
}

public String toString(){
	int[]arr=coefficient();
	return ("best: "+"a= "+arr[0]+" b= "+arr[1]+" c= "+arr[2]+" d="+arr[3]+" e="+arr[4]+"fitness:"+ evaluate());
}




}	